package de.jmda.gen.java.impl;

import org.apache.log4j.Logger;

import de.jmda.gen.GeneratorException;
import de.jmda.gen.java.DeclaredInstanceMethodGenerator;
import de.jmda.gen.java.MethodBodyGenerator;
import de.jmda.gen.java.MethodModifiersGenerator;
import de.jmda.gen.java.MethodNameGenerator;
import de.jmda.gen.java.MethodParametersGenerator;
import de.jmda.gen.java.ThrowsClauseGenerator;
import de.jmda.gen.java.TypeNameGenerator;

public class DefaultDeclaredInstanceMethodGenerator
		extends AbstractDeclaredMethodGenerator
		implements DeclaredInstanceMethodGenerator
{
	private final static Logger LOGGER =
      Logger.getLogger(DefaultDeclaredInstanceMethodGenerator.class);

	private boolean interfaceMethodGenerator;

	public DefaultDeclaredInstanceMethodGenerator(
      MethodModifiersGenerator modifiersGenerator,
      TypeNameGenerator typeNameGenerator,
      MethodNameGenerator methodNameGenerator,
      MethodParametersGenerator methodParameterListGenerator,
      ThrowsClauseGenerator throwsClauseGenerator,
      MethodBodyGenerator methodBodyGenerator)
  {
	  super(
	  		modifiersGenerator,
	  		typeNameGenerator,
	  		methodNameGenerator,
	      methodParameterListGenerator,
	      throwsClauseGenerator,
	      methodBodyGenerator);
	  
	  modifiersGenerator.setStatic(false);
  }

	public DefaultDeclaredInstanceMethodGenerator()
  {
		super();
  }

	@Override
	public StringBuffer generate() throws GeneratorException
	{
		MethodModifiersGenerator modifiersGenerator = demandModifiersGenerator();
		
		if (modifiersGenerator.isStatic())
		{
			LOGGER.warn(
					"'static' modifier not allowed for instance method, " +
					"will omit modifier");
			modifiersGenerator.setStatic(false);
		}
		
		if ((false == modifiersGenerator.isAbstract()) &&
		    (false == isInterfaceMethodGenerator()))
		{
			// make sure non abstract method declarations have a method body
			demandMethodBodyGenerator();
		}
		else
		{
			setMethodBodyGenerator(null);
		}
		
		return super.generate();
	}

	@Override
  public boolean isInterfaceMethodGenerator()
  {
	  return interfaceMethodGenerator;
  }

	@Override
  public void setInterfaceMethodGenerator(boolean isInterfaceMethodGenerator)
  {
		interfaceMethodGenerator = isInterfaceMethodGenerator;
  }
}